<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 8.8: Simplest linear discrimination</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch08_geometric_probs/html/linear_discr.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 8.8: Simplest linear discrimination</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/16/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The goal is to find a function f(x) = a'*x - b that classifies the points</span>
<span class="comment">% {x_1,...,x_N} and {y_1,...,y_M}. a and b can be obtained by solving a</span>
<span class="comment">% feasibility problem:</span>
<span class="comment">%           minimize    0</span>
<span class="comment">%               s.t.    a'*x_i - b &gt;=  1     for i = 1,...,N</span>
<span class="comment">%                       a'*y_i - b &lt;= -1     for i = 1,...,M</span>

<span class="comment">% data generation</span>
n = 2;
randn(<span class="string">'state'</span>,3);
N = 10; M = 6;
Y = [1.5+1*randn(1,M); 2*randn(1,M)];
X = [-1.5+1*randn(1,N); 2*randn(1,N)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

<span class="comment">% Solution via CVX</span>
fprintf(<span class="string">'Finding a separating hyperplane...'</span>);

cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span>
    X'*a - b &gt;= 1;
    Y'*a - b &lt;= -1;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
linewidth = 0.5;  <span class="comment">% for the squares and circles</span>
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
t = linspace(t_min-1,t_max+1,100);
p = -a(1)*t/a(2) + b/a(2);

graph = plot(X(1,:),X(2,:), <span class="string">'o'</span>, Y(1,:), Y(2,:), <span class="string">'o'</span>);
set(graph(1),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'MarkerFaceColor'</span>,[0 0.5 0]);
hold <span class="string">on</span>;
plot(t,p, <span class="string">'-r'</span>);
axis <span class="string">equal</span>
title(<span class="string">'Simple classification using an affine function'</span>);
<span class="comment">% print -deps lin-discr.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
Finding a separating hyperplane... 
Calling Mosek 9.1.9: 16 variables, 3 equality constraints
   For improved efficiency, Mosek is solving the dual problem.
------------------------------------------------------------

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : LO (linear optimization problem)
  Constraints            : 3               
  Cones                  : 0               
  Scalar variables       : 16              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 2                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : LO (linear optimization problem)
  Constraints            : 3               
  Cones                  : 0               
  Scalar variables       : 16              
  Matrix variables       : 0               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 3
Optimizer  - Cones                  : 0
Optimizer  - Scalar variables       : 16                conic                  : 0               
Optimizer  - Semi-definite variables: 0                 scalarized             : 0               
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 6                 after factor           : 6               
Factor     - dense dim.             : 0                 flops                  : 2.06e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   7.2e+00  2.8e+00  2.1e+01  0.00e+00   -1.600000000e+01  0.000000000e+00   2.0e+00  0.00  
1   1.6e+00  6.2e-01  4.6e+00  -2.36e+00  -5.398997601e+04  0.000000000e+00   4.4e-01  0.01  
2   2.0e-01  7.8e-02  5.9e-01  -9.52e-02  -4.191335959e+02  0.000000000e+00   5.5e-02  0.01  
3   1.1e-03  4.2e-04  3.1e-03  8.17e-01   -2.512600010e+00  0.000000000e+00   3.0e-04  0.01  
4   1.1e-07  4.2e-08  3.1e-07  9.99e-01   -2.516481137e-04  0.000000000e+00   3.0e-08  0.01  
5   1.1e-11  4.2e-12  3.1e-11  1.00e+00   -2.516481278e-08  0.000000000e+00   3.0e-12  0.01  
6   1.1e-15  1.0e-15  3.1e-15  1.00e+00   -2.516481278e-12  0.000000000e+00   3.0e-16  0.01  
Basis identification started.
Primal basis identification phase started.
Primal basis identification phase terminated. Time: 0.00
Dual basis identification phase started.
Dual basis identification phase terminated. Time: 0.00
Basis identification terminated. Time: 0.00
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -2.5164812778e-12   nrm: 8e-13    Viol.  con: 2e-12    var: 0e+00  
  Dual.    obj: 0.0000000000e+00    nrm: 2e+03    Viol.  con: 0e+00    var: 2e-13  

Basic solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 0.0000000000e+00    nrm: 0e+00    Viol.  con: 0e+00    var: 0e+00  
  Dual.    obj: 0.0000000000e+00    nrm: 1e+01    Viol.  con: 0e+00    var: 2e-13  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 6         time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 3         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0
 
Done! 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="linear_discr__01.png" alt=""> 
</div>
</div>
</body>
</html>